package emf2gwtbridge.generators;

import emf2gwtbridge.*;
import org.eclipse.emf.codegen.ecore.genmodel.*;

public class Proxy
{
  protected static String nl;
  public static synchronized Proxy create(String lineSeparator)
  {
    nl = lineSeparator;
    Proxy result = new Proxy();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package ";
  protected final String TEXT_2 = ";";
  protected final String TEXT_3 = NL + NL + NL + "@";
  protected final String TEXT_4 = "(value=";
  protected final String TEXT_5 = ".class, locator =";
  protected final String TEXT_6 = ".class)" + NL + "public interface ";
  protected final String TEXT_7 = " extends ";
  protected final String TEXT_8 = NL;
  protected final String TEXT_9 = ", ";
  protected final String TEXT_10 = NL + "{\t" + NL;
  protected final String TEXT_11 = NL + "  " + NL + "\t";
  protected final String TEXT_12 = " get";
  protected final String TEXT_13 = "();" + NL + "\tvoid set";
  protected final String TEXT_14 = "(";
  protected final String TEXT_15 = " value);";
  protected final String TEXT_16 = NL + "  " + NL + "\t";
  protected final String TEXT_17 = " get";
  protected final String TEXT_18 = "();" + NL + "\tvoid set";
  protected final String TEXT_19 = "(";
  protected final String TEXT_20 = " value);";
  protected final String TEXT_21 = NL + "     " + NL + "\t";
  protected final String TEXT_22 = " get";
  protected final String TEXT_23 = "();" + NL + "\tvoid set";
  protected final String TEXT_24 = "(";
  protected final String TEXT_25 = " value);";
  protected final String TEXT_26 = NL + "}";
  protected final String TEXT_27 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    GenClass genClass = (GenClass)argument; GenPackage genPackage = genClass.getGenPackage(); GenModel genModel = genPackage.getGenModel();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(GeneratorUtils.getProxyPackage(genPackage));
    stringBuffer.append(TEXT_2);
    genModel.markImportLocation(stringBuffer, genPackage);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(genModel.getImportedName("com.google.web.bindery.requestfactory.shared.ProxyFor"));
    stringBuffer.append(TEXT_4);
    stringBuffer.append(genClass.getImportedClassName());
    stringBuffer.append(TEXT_5);
    stringBuffer.append(genModel.getImportedName(GeneratorUtils.getServicePackage(genPackage)+ "." + GeneratorUtils.getServiceClassName(genClass)));
    stringBuffer.append(TEXT_6);
    stringBuffer.append(GeneratorUtils.getProxyClassName(genClass));
    stringBuffer.append(TEXT_7);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(genModel.getImportedName("com.google.web.bindery.requestfactory.shared.EntityProxy"));
     for (GenClass s: genClass.getBaseGenClasses()) {
    stringBuffer.append(TEXT_9);
    stringBuffer.append(genModel.getImportedName(s.getClassName() + "Proxy"));
     } 
    stringBuffer.append(TEXT_10);
    for (GenFeature genFeature : genClass.getGenFeatures()) {
    if (genFeature.isListType()) { 
    stringBuffer.append(TEXT_11);
    stringBuffer.append(GeneratorUtils.getType(genFeature));
    stringBuffer.append(TEXT_12);
    stringBuffer.append( genFeature.getAccessorName());
    stringBuffer.append(TEXT_13);
    stringBuffer.append(genFeature.getAccessorName());
    stringBuffer.append(TEXT_14);
    stringBuffer.append(GeneratorUtils.getType(genFeature));
    stringBuffer.append(TEXT_15);
    } else 
    if ("attribute".equals(genFeature.getFeatureKind())) { 
    stringBuffer.append(TEXT_16);
    stringBuffer.append(genFeature.getImportedType(null));
    stringBuffer.append(TEXT_17);
    stringBuffer.append(genFeature.getAccessorName());
    stringBuffer.append(TEXT_18);
    stringBuffer.append(genFeature.getAccessorName());
    stringBuffer.append(TEXT_19);
    stringBuffer.append(genFeature.getImportedType(null));
    stringBuffer.append(TEXT_20);
    } else { 
    stringBuffer.append(TEXT_21);
    stringBuffer.append(genModel.getImportedName(genFeature.getImportedType(null) + "Proxy"));
    stringBuffer.append(TEXT_22);
    stringBuffer.append(genFeature.getAccessorName());
    stringBuffer.append(TEXT_23);
    stringBuffer.append(genFeature.getAccessorName());
    stringBuffer.append(TEXT_24);
    stringBuffer.append(genModel.getImportedName(genFeature.getImportedType(null) + "Proxy"));
    stringBuffer.append(TEXT_25);
    }
    }
    stringBuffer.append(TEXT_26);
    genModel.emitSortedImports();
    stringBuffer.append(TEXT_27);
    return stringBuffer.toString();
  }
}
